package com.ug_project.views


import android.app.Activity
import android.support.v4.view.ViewPager
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.*
import android.widget.Button
import android.widget.CheckedTextView
import android.widget.LinearLayout
import android.widget.RadioButton
import android.widget.RelativeLayout
import android.widget.TextView
import com.ug_project.Animation.errAnim
import com.ug_project.Async.runASync
import java.util.*


var<T:View> T.click: (view: T) -> Unit
    get() = {Exception("بۇ خاسلىقنى ئىلىشنى قوللىمايدۇ")}
    set(value) {
        val action:(View)->Unit ={
            value(it as T)
        }
        this.setOnClickListener(action)
    }

fun <T : View> T.click(clickAction: (view: T) -> Unit){
    this.click = clickAction
}

var<T:View> T.longClick: (view: T) -> Boolean
    get() = {Exception("بۇ خاسلىقنى ئىلىشنى قوللىمايدۇ");false}
    set(value) {
        val action:(View)->Boolean ={
            value(it as T)
        }
        this.setOnLongClickListener(action)
    }

fun <T : View> T.longClick(clickAction: (view: T) -> Boolean){
    this.longClick = clickAction
}


var TextView.isEmpty: Boolean
get() = this.text.toString().isNullOrEmpty()
set(value) {}

var TextView.content: String
get() = this.text.toString()
set(value) {this.text = value}

fun View.findView(Resource_ID: Int): View = this.findViewById(Resource_ID)
fun Activity.findView(Resource_ID: Int): View = this.findViewById(Resource_ID)
fun <T> View.findView(id: Int): T = findView(id) as T
fun <T> Activity.findView(id: Int): T = findView(id) as T
fun View.findButton(Resource_ID: Int) : android.widget.Button = findView<Button>(Resource_ID)
fun View.findTextView(Resource_ID: Int) : android.widget.TextView = findView<TextView>(Resource_ID)
fun View.findEditText(Resource_ID: Int) : android.widget.EditText = findView<android.widget.EditText>(Resource_ID)
fun View.findCheckBox(Resource_ID: Int) : android.widget.CheckBox = findView<android.widget.CheckBox>(Resource_ID)
fun View.findCheckedTextView(Resource_ID: Int) : CheckedTextView = findView<android.widget.CheckedTextView>(Resource_ID)
fun View.findRadioButton(Resource_ID: Int) : RadioButton = findView<android.widget.RadioButton>(Resource_ID)
fun View.findListView(Resource_ID: Int) : ListView = findView<android.widget.ListView>(Resource_ID)
fun View.findRecyclerView(Resource_ID: Int) : RecyclerView = findView<RecyclerView>(Resource_ID)
fun View.findLinearLayout(Resource_ID: Int) : LinearLayout = findView<LinearLayout>(Resource_ID)
fun View.findRelativeLayout(Resource_ID: Int) : RelativeLayout = findView<RelativeLayout>(Resource_ID)
fun View.findViewPager(Resource_ID: Int) : ViewPager = findView<ViewPager>(Resource_ID)

val View.meaSuredHeight: Int
    get() {
        val msp = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
        this.measure(msp, msp)
        return this.measuredHeight
    }

val View.meaSuredWidth: Int
    get(){
        val msp = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
        this.measure(msp, msp)
        return this.measuredWidth
    }


//كونۇپكا تاختىسىنى يۇشۇرۇش
fun View.hideKeyboard() {
    this.context.inputManager.hideSoftInputFromWindow(applicationWindowToken, 0)
}

fun View.showKeyBoard() {
    this.context.inputManager.showSoftInput(this, 0)
}

private fun handleEmptyEdit(edit: android.widget.EditText) {
    edit.requestFocus();edit.showKeyBoard();edit.errAnim()
}

fun EditText.checkEdit(notEmpty: () -> Unit) {
    if (this.isEmpty) {
        handleEmptyEdit(this)
        return
    }
    notEmpty()
}

fun Array<android.widget.EditText>.clearEdits() {
    this.forEach {
        it.content = ""
    }
}

fun android.widget.EditText.comparePwd(pwd: android.widget.EditText, action: () -> Unit, err: ((edit1: android.widget.EditText, edit2: android.widget.EditText) -> Unit)? = null) {
    if (this.content.trim().contentEquals(pwd.content.trim())) {
        action()
        return
    }
    if (err == null) {
//        this.anim = Techniques.Swing
//        pwd.anim = Techniques.Swing
        return
    }
    err(this, pwd)
}

/**
 * @param data
 * @param action
 *  شەرتكە ئاساسەن spinner نى ئاپتۇماتىك تاللايدۇ
 */
fun <T> Spinner.selection(data: ArrayList<T>, action: (position: Int) -> Boolean) {
    for (i in data.indices) {
        if (action.invoke(i)) {
            this.setSelection(i)
            break
        }
    }
}

fun Array<android.widget.EditText>.checkEdits(notEmpty: () -> Unit) {
    forEach {
        if (it.isEmpty) {
            handleEmptyEdit(it)
            return
        }
    }
    notEmpty()
}

fun android.widget.EditText.selectionEnd() {
    this.setSelection(this.text.toString().length)
}

fun TextView.countTime(time: Int = 60, text: String = "تەستىق نۇمۇر يوللاڭ", action: (() -> Unit)? = null) {
    this.isEnabled = false
    runASync {
        for (i in time downTo 0) {
            runUI {
                this@countTime.text = "$i s"
            }
            delay(1000)
        }
        runUI {
            this@countTime.isEnabled = true
            this@countTime.text = text
            action?.invoke()
        }

    }
}

